﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Oracle.ManagedDataAccess.Client;

namespace YJ.Data.ORACLE
{
    public class ProgramBuilderExport : YJ.Data.Interface.IProgramBuilderExport
    {
        private DBHelper dbHelper = new DBHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public ProgramBuilderExport()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.ProgramBuilderExport实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(YJ.Data.Model.ProgramBuilderExport model)
        {
            string sql = @"INSERT INTO programbuilderexport
				(ID,ProgramID,Field,ShowTitle,Align,Width,ShowType,DataType,ShowFormat,CustomString,Sort) 
				VALUES(:ID,:ProgramID,:Field,:ShowTitle,:Align,:Width,:ShowType,:DataType,:ShowFormat,:CustomString,:Sort)";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2, 36){ Value = model.ID },
				new OracleParameter(":ProgramID", OracleDbType.Varchar2, 36){ Value = model.ProgramID },
				new OracleParameter(":Field", OracleDbType.Blob, -1){ Value = model.Field },
				model.ShowTitle == null ? new OracleParameter(":ShowTitle", OracleDbType.Blob, -1) { Value = DBNull.Value } : new OracleParameter(":ShowTitle", OracleDbType.Blob, -1) { Value = model.ShowTitle },
				new OracleParameter(":Align", OracleDbType.Int32, 11){ Value = model.Align },
				model.Width == null ? new OracleParameter(":Width", OracleDbType.Int32, 11) { Value = DBNull.Value } : new OracleParameter(":Width", OracleDbType.Int32, 11) { Value = model.Width },
				model.ShowType == null ? new OracleParameter(":ShowType", OracleDbType.Int32, 11) { Value = DBNull.Value } : new OracleParameter(":ShowType", OracleDbType.Int32, 11) { Value = model.ShowType },
				model.DataType == null ? new OracleParameter(":DataType", OracleDbType.Int32, 11) { Value = DBNull.Value } : new OracleParameter(":DataType", OracleDbType.Int32, 11) { Value = model.DataType },
				model.ShowFormat == null ? new OracleParameter(":ShowFormat", OracleDbType.Varchar2, 50) { Value = DBNull.Value } : new OracleParameter(":ShowFormat", OracleDbType.Varchar2, 50) { Value = model.ShowFormat },
				model.CustomString == null ? new OracleParameter(":CustomString", OracleDbType.Blob, -1) { Value = DBNull.Value } : new OracleParameter(":CustomString", OracleDbType.Blob, -1) { Value = model.CustomString },
				new OracleParameter(":Sort", OracleDbType.Int32, 11){ Value = model.Sort }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.ProgramBuilderExport实体类</param>
        public int Update(YJ.Data.Model.ProgramBuilderExport model)
        {
            string sql = @"UPDATE programbuilderexport SET 
				ProgramID=:ProgramID,Field=:Field,ShowTitle=:ShowTitle,Align=:Align,Width=:Width,ShowType=:ShowType,DataType=:DataType,ShowFormat=:ShowFormat,CustomString=:CustomString,Sort=:Sort
				WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ProgramID", OracleDbType.Varchar2, 36){ Value = model.ProgramID },
				new OracleParameter(":Field", OracleDbType.Blob, -1){ Value = model.Field },
				model.ShowTitle == null ? new OracleParameter(":ShowTitle", OracleDbType.Blob, -1) { Value = DBNull.Value } : new OracleParameter(":ShowTitle", OracleDbType.Blob, -1) { Value = model.ShowTitle },
				new OracleParameter(":Align", OracleDbType.Int32, 11){ Value = model.Align },
				model.Width == null ? new OracleParameter(":Width", OracleDbType.Int32, 11) { Value = DBNull.Value } : new OracleParameter(":Width", OracleDbType.Int32, 11) { Value = model.Width },
				model.ShowType == null ? new OracleParameter(":ShowType", OracleDbType.Int32, 11) { Value = DBNull.Value } : new OracleParameter(":ShowType", OracleDbType.Int32, 11) { Value = model.ShowType },
				model.DataType == null ? new OracleParameter(":DataType", OracleDbType.Int32, 11) { Value = DBNull.Value } : new OracleParameter(":DataType", OracleDbType.Int32, 11) { Value = model.DataType },
				model.ShowFormat == null ? new OracleParameter(":ShowFormat", OracleDbType.Varchar2, 50) { Value = DBNull.Value } : new OracleParameter(":ShowFormat", OracleDbType.Varchar2, 50) { Value = model.ShowFormat },
				model.CustomString == null ? new OracleParameter(":CustomString", OracleDbType.Blob, -1) { Value = DBNull.Value } : new OracleParameter(":CustomString", OracleDbType.Blob, -1) { Value = model.CustomString },
				new OracleParameter(":Sort", OracleDbType.Int32, 11){ Value = model.Sort },
				new OracleParameter(":ID", OracleDbType.Varchar2, 36){ Value = model.ID }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(Guid id)
        {
            string sql = "DELETE FROM programbuilderexport WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2, 36){ Value = id.ToString() }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<YJ.Data.Model.ProgramBuilderExport> DataReaderToList(OracleDataReader dataReader)
        {
            List<YJ.Data.Model.ProgramBuilderExport> List = new List<YJ.Data.Model.ProgramBuilderExport>();
            YJ.Data.Model.ProgramBuilderExport model = null;
            while (dataReader.Read())
            {
                model = new YJ.Data.Model.ProgramBuilderExport();
                model.ID = dataReader.GetString(0).ToGuid();
                model.ProgramID = dataReader.GetString(1).ToGuid();
                model.Field = dataReader.GetString(2);
                if (!dataReader.IsDBNull(3))
                    model.ShowTitle = dataReader.GetString(3);
                model.Align = dataReader.GetInt32(4);
                if (!dataReader.IsDBNull(5))
                    model.Width = dataReader.GetInt32(5);
                if (!dataReader.IsDBNull(6))
                    model.ShowType = dataReader.GetInt32(6);
                if (!dataReader.IsDBNull(7))
                    model.DataType = dataReader.GetInt32(7);
                if (!dataReader.IsDBNull(8))
                    model.ShowFormat = dataReader.GetString(8);
                if (!dataReader.IsDBNull(9))
                    model.CustomString = dataReader.GetString(9);
                model.Sort = dataReader.GetInt32(10);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.ProgramBuilderExport> GetAll()
        {
            string sql = "SELECT * FROM programbuilderexport";
            OracleDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.ProgramBuilderExport> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM programbuilderexport";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public YJ.Data.Model.ProgramBuilderExport Get(Guid id)
        {
            string sql = "SELECT * FROM programbuilderexport WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2, 36){ Value = id.ToString() }
			};
            OracleDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.ProgramBuilderExport> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }


        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.ProgramBuilderExport> GetAll(Guid programID)
        {
            string sql = "SELECT * FROM ProgramBuilderExport WHERE ProgramID=:ProgramID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ProgramID", OracleDbType.Varchar2){ Value = programID }
			};
            OracleDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.ProgramBuilderExport> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
    }
}